package com.yu.dv.strategy;

import com.yu.dv.strategy.metadata.MetadataStrategyFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 简化的策略工厂构建器
 */
public class StrategyFactoryBuilder {

    private static final Map<String, Object> FACTORIES = new ConcurrentHashMap<>();
    private static final String METADATA_FACTORY_KEY = "MetadataStrategyFactory";

    static {
        // 静态初始化时注册工厂
        FACTORIES.put(METADATA_FACTORY_KEY, MetadataStrategyFactory.getInstance());
    }

    private StrategyFactoryBuilder() {
        // 私有构造函数
    }

    /**
     * 获取元数据策略工厂（直接类型转换）
     */
    public static MetadataStrategyFactory getMetadataStrategyFactory() {
        return getFactory(METADATA_FACTORY_KEY, MetadataStrategyFactory.class);
    }


    /**
     * 注册其他工厂
     */
    public static void registerFactory(String key, Object factory) {
        FACTORIES.put(key, factory);
    }

    /**
     * 获取工厂
     */
    @SuppressWarnings("unchecked")
    public static <T> T getFactory(String key, Class<T> factoryType) {
        Object factory = FACTORIES.get(key);
        if (factoryType.isInstance(factory)) {
            return (T) factory;
        }
        return null;
    }
}